package com.quan.system.common.mq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.quan.common.base.constant.CommonConstant;
import com.quan.common.utils.RandomUitl;
import com.quan.system.entity.SystemUser;

/**
 * 消息生产端
 * 
 * @author yhaoquan
 *
 */
@Component
public class TestMessageProducer01 implements RabbitTemplate.ReturnCallback {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private RabbitTemplate rabbitTemplate;

	/**
	 * 发送日志消息
	 * 
	 * @param entity
	 */
	public void sender(SystemUser entity) {
		logger.info("-----------------------TestMessageProducer01>>>消息生产端----------------------------");
		
		this.rabbitTemplate.setReturnCallback(this);
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
        	System.out.println(ack);
            if (!ack) {
                System.out.println("HelloSender消息发送失败" + cause + correlationData.toString());
            } else {
                System.out.println("HelloSender 消息发送成功 ");
            }
        });
		
		// 发送消息(交换机 exchange, 路由键 routingKey, 消息体内容, 消息唯一ID)
		this.rabbitTemplate.convertAndSend(
				CommonConstant.EXCHANGE_TOPIC_TEST,
				CommonConstant.QUEUE_TOPIC_TEST+".TestMessageConsumer01",
				entity, 
				new CorrelationData(RandomUitl.uuid()));
		
	}

	@Override
	public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
		System.out.println("sender return success" + message.toString()+"==="+replyCode+"==="+exchange+"==="+routingKey);
	}

}
